FrameLib  0.1
Arbitrarily timed and sized frame-based DSP
FrameLib_DSP.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_DSP_H
3 #define FRAMELIB_DSP_H
4 
5 #include "FrameLib_Types.h"
6 #include "FrameLib_Context.h"
7 #include "FrameLib_Object.h"
8 #include "FrameLib_DSPQueue.h"
9 #include <limits>
10 #include <vector>
11 #include <algorithm>
12 
13 // FrameLib_DSP
14 
15 // This abstract class is the core of the DSP processing system and handles low level single channel connections and timing
16 
18 {
19  // Type definition for concision / Queue access
20 
22  friend class FrameLib_DSPQueue;
23 
24 protected:
25 
27  {
29  : mTimeAdvance(0), mNewFrame(false), mOutputDone(false) {}
30 
31  SchedulerInfo(FrameLib_TimeFormat timeAdvance, bool newFrame, bool outputDone)
32  : mTimeAdvance(timeAdvance), mNewFrame(newFrame), mOutputDone(outputDone) {}
33 
35  bool mNewFrame;
37  };
38 
39 private:
40 
41  struct Input
42  {
43  Input() : mObject(NULL), mIndex(0), mSize(0), mFixedInput(NULL), mType(kFrameNormal), mUpdate(false), mParameters(false), mTrigger(true), mSwitchable(false) {}
44 
45  void setInput(FrameLib_DSP *object, unsigned long idx)
46  {
47  mObject = object;
48  mIndex = idx;
49  }
50 
51  void setInput()
52  {
53  setInput(NULL, 0);
54  }
55 
56  // Connection Info
57 
58  FrameLib_DSP *mObject;
59  unsigned long mIndex;
60 
61  // Fixed Input
62 
63  unsigned long mSize;
64  double *mFixedInput;
65 
66  // Flags
67 
68  FrameType mType;
69 
70  bool mUpdate;
71  bool mParameters;
72  bool mTrigger;
73  bool mSwitchable;
74  };
75 
76  struct Output
77  {
78  Output() : mMemory(NULL), mType(kFrameNormal), mCurrentSize(0), mRequestedSize(0), mPointerOffset(0) {}
79 
80  void *mMemory;
81 
82  FrameType mType;
83 
84  size_t mCurrentSize;
85  size_t mRequestedSize;
86  size_t mPointerOffset;
87  };
88 
89 public:
90 
91  // Constructor / Destructor
92 
93  FrameLib_DSP(ObjectType type, FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans = 0);
94  ~FrameLib_DSP();
95 
96  // Set Fixed Inputs
97 
98  virtual void setFixedInput(unsigned long idx, double *input, unsigned long size);
99 
100  // Audio Processing
101 
102  virtual void blockUpdate(double **ins, double **outs, unsigned long blockSize);
103  virtual void reset(double samplingRate, unsigned long maxBlockSize);
104 
105  // Connection Methods
106 
107  // N.B. - No sanity checks here to maximise speed / help debugging (better for it to crash if a mistake is made)
108 
109  virtual void deleteConnection(unsigned long inIdx);
110  virtual void addConnection(FrameLib_DSP *object, unsigned long outIdx, unsigned long inIdx);
111  virtual void clearConnections();
112  virtual bool isConnected(unsigned long inIdx);
113 
114  // Info (individual objects should override other methods to provide info)
115 
116  virtual const FrameLib_Parameters *getParameters() { return &mParameters; }
117 
118  virtual FrameType inputType(unsigned long idx) { return mInputs[idx].mType; }
119  virtual FrameType outputType(unsigned long idx) { return mOutputs[idx].mType; }
120 
121 protected:
122 
123  // Setup and IO Modes
124 
125  // Call these from your constructor only (unsafe elsewhere)
126 
127  void setIO(unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans = 0);
128  void inputMode(unsigned long idx, bool update, bool trigger, bool switchable, FrameType type = kFrameNormal);
129  void setParameterInput(unsigned long idx);
130  void addParameterInput();
131  void outputMode(unsigned long idx, FrameType type);
132 
133  // You should only call this from your update method (it is unsafe anywhere else)
134 
135  void updateTrigger(unsigned long idx, bool trigger);
136 
137  // Processing Utilities
138 
139  // Test if an Input Triggered the Current Frame
140 
141  bool isTrigger(unsigned long idx) { return mInputs[idx].mTrigger && mInputs[idx].mObject && (mInputs[idx].mObject->mFrameTime == mFrameTime); }
142 
143  // Timing
144 
145  FrameLib_TimeFormat getFrameTime() { return mFrameTime; }
146  FrameLib_TimeFormat getValidTime() { return mValidTime; }
147  FrameLib_TimeFormat getInputTime() { return mInputTime; }
148  FrameLib_TimeFormat getCurrentTime() { return getType() == kScheduler ? mValidTime : mFrameTime; }
149  FrameLib_TimeFormat getBlockStartTime() { return getType() == kOutput ? mBlockEndTime : mBlockStartTime; }
150  FrameLib_TimeFormat getBlockEndTime() { return mBlockEndTime; }
151 
152  FrameLib_TimeFormat getInputFrameTime(unsigned long idx) { return mInputs[idx].mObject ? mInputs[idx].mObject->mFrameTime : FrameLib_TimeFormat(0); }
153  FrameLib_TimeFormat getInputValidTime(unsigned long idx) { return mInputs[idx].mObject ? mInputs[idx].mObject->mValidTime : FrameLib_TimeFormat(0); }
154 
155  // Output Allocation
156 
157  void requestOutputSize(unsigned long idx, size_t size) { mOutputs[idx].mRequestedSize = size; }
158  bool allocateOutputs();
159 
160  // Get Inputs and Outputs
161 
162  double *getInput(unsigned long idx, size_t *size);
163  FrameLib_Parameters::Serial *getInput(unsigned long idx);
164 
165  double *getOutput(unsigned long idx, size_t *size);
166  FrameLib_Parameters::Serial *getOutput(unsigned long idx);
167 
168  // Convience methods for copying and zeroing
169 
170  void copyVector(double *output, double *input, unsigned long size) { std::copy(input, input + size, output); }
171  void zeroVector(double *output, unsigned long size) { std::fill_n(output, size, 0.0); }
172 
173  // Get DSP Object for a Given Output
174 
175  FrameLib_DSP *getOutputObject(unsigned long outIdx) { return this; }
176 
177 private:
178 
179  // Deleted
180 
181  FrameLib_DSP(const FrameLib_DSP&);
182  FrameLib_DSP& operator=(const FrameLib_DSP&);
183 
184  // Customisable Processing
185 
186  // Override to handle audio at the block level
187 
188  virtual void blockProcess(double **ins, double **outs, unsigned long blockSize) {}
189 
190  // Override to get called on audio reset
191 
192  virtual void objectReset() {}
193 
194  // Override for updates prior to schedule / process (e.g. adjusting triggers)
195 
196  virtual void update() {}
197 
198  // Override for scheduling code (scheduler objects must override this)
199 
200  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) = 0;
201 
202  // Override for main frame processing code (processor objects must override this)
203 
204  virtual void process() = 0;
205 
206  // Scheduling
207 
208  // This returns true if the object requires notification from an audio thread (is a scheduler/has audio input)
209 
210  bool requiresAudioNotification() { return getType() == kScheduler || getNumAudioIns(); }
211 
212  // Manage Output Memory
213 
214  inline void freeOutputMemory();
215  inline void releaseOutputMemory();
216 
217  // Dependency Notification
218 
219  inline void dependencyNotify(bool releaseMemory, bool fromInput);
220  void dependenciesReady();
221  void setOutputDependencyCount();
222  void incrementInputDependency();
223  void resetDependencyCount();
224 
225  // Dependency Updating
226 
227  std::vector <FrameLib_DSP *>::iterator removeInputDependency(FrameLib_DSP *object);
228  std::vector <FrameLib_DSP *>::iterator removeOutputDependency(FrameLib_DSP *object);
229  void addInputDependency(FrameLib_DSP *object);
230  void addOutputDependency(FrameLib_DSP *object);
231 
232  // Connection Methods (private)
233 
234  void clearConnection(unsigned long inIdx);
235  void removeConnection(unsigned long inIdx);
236  std::vector <FrameLib_DSP *>::iterator disconnect(FrameLib_DSP *object);
237 
238 protected:
239 
240  // Member Variables
241 
242  // Sampling Rate and Maximum Block Size
243 
245  unsigned long mMaxBlockSize;
246 
247  // Memory Allocator
248 
250 
251  // Parameters
252 
254 
255 private:
256 
257  // DSP Queue
258 
260  FrameLib_DSP *mNext;
261 
262  // IO Info
263 
264  std::vector <Input> mInputs;
265  std::vector <Output> mOutputs;
266 
267  std::vector <FrameLib_DSP *> mInputDependencies;
268  std::vector <FrameLib_DSP *> mOutputDependencies;
269 
270  // Dependency Counts
271 
272  long mInputCount;
273  long mDependencyCount;
274  long mOutputMemoryCount;
275 
276  // Frame and Block Timings
277 
278  FrameLib_TimeFormat mFrameTime;
279  FrameLib_TimeFormat mValidTime;
280  FrameLib_TimeFormat mInputTime;
281  FrameLib_TimeFormat mBlockStartTime;
282  FrameLib_TimeFormat mBlockEndTime;
283 
284  bool mUpdatingInputs;
285  bool mInUpdate;
286  bool mOutputDone;
287 };
288 
289 // ************************************************************************************** //
290 
291 // FrameLib_Processor - Simple class for process type objects (can't handle audio)
292 
294 {
295 
296 public:
297 
298  FrameLib_Processor(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0)
299  : FrameLib_DSP(kProcessor, context, info, nIns, nOuts) {}
300 
301  static ObjectType getType() { return kProcessor; }
302  static bool handlesAudio() { return false; }
303 
304 protected:
305 
306  // This prevents the user from needing to implement this method - doing so will do nothing
307 
308  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) { return SchedulerInfo(); }
309 
310  void setIO(unsigned long nIns, unsigned long nOuts) { FrameLib_DSP::setIO(nIns, nOuts); }
311 };
312 
313 // ************************************************************************************** //
314 
315 // FrameLib_AudioInput - Simple class for process type objects (can handle audio input)
316 
318 {
319 
320 public:
321 
322  FrameLib_AudioInput(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0, unsigned long nAudioIns = 0)
323  : FrameLib_DSP(kProcessor, context, info, nIns, nOuts, nAudioIns) {}
324 
325  static ObjectType getType() { return kProcessor; }
326  static bool handlesAudio() { return true; }
327 
328 protected:
329 
330  // This prevents the user from needing to implement this method - doing so will do nothing
331 
332  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) { return SchedulerInfo(); }
333 };
334 
335 // ************************************************************************************** //
336 
337 // FrameLib_AudioOutput - Simple class for process type objects (can handle audio output)
338 
340 {
341 
342 public:
343 
344  FrameLib_AudioOutput(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0, unsigned long nAudioOuts = 0)
345  : FrameLib_DSP(kOutput, context, info, nIns, nOuts, nAudioOuts) {}
346 
347  static ObjectType getType() { return kOutput; }
348  static bool handlesAudio() { return true; }
349 
350 protected:
351 
352  // This prevents the user from needing to implement this method - doing so will do nothing
353 
354  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) { return SchedulerInfo(); }
355 };
356 
357 // ************************************************************************************** //
358 
359 // FrameLib_Scheduler - Simple class for scheduler type objects
360 
362 {
363 
364 public:
365 
366  FrameLib_Scheduler(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0, unsigned long nAudioIns = 0)
367  : FrameLib_DSP(kScheduler, context, info, nIns, nOuts, nAudioIns) {}
368 
369  static ObjectType getType() { return kScheduler; }
370  static bool handlesAudio() { return true; }
371 
372 protected:
373 
374  // This prevents the user from needing to implement this method - doing so will do nothing
375 
376  virtual void process() {}
377 };
378 
379 #endif
FrameLib_TimeFormat getFrameTime()
Definition: FrameLib_DSP.h:145
ObjectType
Definition: FrameLib_Types.h:24
static bool handlesAudio()
Definition: FrameLib_DSP.h:348
Definition: FrameLib_Types.h:24
Definition: FrameLib_Parameters.h:21
bool allocateOutputs()
Definition: FrameLib_DSP.cpp:206
FL_FP FrameLib_TimeFormat
Definition: FrameLib_Types.h:20
Definition: FrameLib_Types.h:25
Definition: FrameLib_Context.h:10
virtual const FrameLib_Parameters * getParameters()
Definition: FrameLib_DSP.h:116
FrameLib_TimeFormat getInputValidTime(unsigned long idx)
Definition: FrameLib_DSP.h:153
static bool handlesAudio()
Definition: FrameLib_DSP.h:302
double * getInput(unsigned long idx, size_t *size)
Definition: FrameLib_DSP.cpp:257
void setParameterInput(unsigned long idx)
Definition: FrameLib_DSP.cpp:170
FrameLib_DSP * getOutputObject(unsigned long outIdx)
Definition: FrameLib_DSP.h:175
virtual void addConnection(FrameLib_DSP *object, unsigned long outIdx, unsigned long inIdx)
Definition: FrameLib_DSP.cpp:99
Definition: FrameLib_Parameters.h:34
FrameLib_TimeFormat getInputTime()
Definition: FrameLib_DSP.h:147
virtual SchedulerInfo schedule(bool newFrame, bool noAdvance)
Definition: FrameLib_DSP.h:332
FrameLib_TimeFormat getCurrentTime()
Definition: FrameLib_DSP.h:148
Definition: FrameLib_DSP.h:17
virtual FrameType inputType(unsigned long idx)
Definition: FrameLib_DSP.h:118
static bool handlesAudio()
Definition: FrameLib_DSP.h:370
FrameLib_TimeFormat mTimeAdvance
Definition: FrameLib_DSP.h:34
Definition: FrameLib_DSP.h:361
virtual void setFixedInput(unsigned long idx, double *input, unsigned long size)
Definition: FrameLib_DSP.cpp:30
FrameLib_AudioInput(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0, unsigned long nAudioIns=0)
Definition: FrameLib_DSP.h:322
FrameLib_Processor(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0)
Definition: FrameLib_DSP.h:298
Definition: FrameLib_DSP.h:317
static ObjectType getType()
Definition: FrameLib_DSP.h:301
Definition: FrameLib_DSP.h:26
unsigned long getNumAudioIns()
Definition: FrameLib_Object.h:40
double * getOutput(unsigned long idx, size_t *size)
Definition: FrameLib_DSP.cpp:274
virtual SchedulerInfo schedule(bool newFrame, bool noAdvance)
Definition: FrameLib_DSP.h:308
double mSamplingRate
Definition: FrameLib_DSP.h:244
FrameLib_TimeFormat getValidTime()
Definition: FrameLib_DSP.h:146
virtual SchedulerInfo schedule(bool newFrame, bool noAdvance)
Definition: FrameLib_DSP.h:354
FrameLib_Parameters mParameters
Definition: FrameLib_DSP.h:253
static ObjectType getType()
Definition: FrameLib_DSP.h:325
Definition: FrameLib_FixedPoint.h:47
ObjectType getType()
Definition: FrameLib_Object.h:27
void inputMode(unsigned long idx, bool update, bool trigger, bool switchable, FrameType type=kFrameNormal)
Definition: FrameLib_DSP.cpp:160
void addParameterInput()
Definition: FrameLib_DSP.cpp:178
void requestOutputSize(unsigned long idx, size_t size)
Definition: FrameLib_DSP.h:157
virtual bool isConnected(unsigned long inIdx)
Definition: FrameLib_DSP.cpp:131
SchedulerInfo()
Definition: FrameLib_DSP.h:28
FrameLib_TimeFormat getInputFrameTime(unsigned long idx)
Definition: FrameLib_DSP.h:152
bool mNewFrame
Definition: FrameLib_DSP.h:35
virtual void process()
Definition: FrameLib_DSP.h:376
void updateTrigger(unsigned long idx, bool trigger)
Definition: FrameLib_DSP.cpp:197
static ObjectType getType()
Definition: FrameLib_DSP.h:369
void zeroVector(double *output, unsigned long size)
Definition: FrameLib_DSP.h:171
Definition: FrameLib_Object.h:95
bool isTrigger(unsigned long idx)
Definition: FrameLib_DSP.h:141
void setIO(unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans=0)
Definition: FrameLib_DSP.cpp:138
size_t blockSize(void *ptr)
Definition: FrameLib_Memory.cpp:23
Definition: FrameLib_Parameters.h:153
Definition: FrameLib_DSP.h:293
virtual void deleteConnection(unsigned long inIdx)
Definition: FrameLib_DSP.cpp:92
void outputMode(unsigned long idx, FrameType type)
Definition: FrameLib_DSP.cpp:190
SchedulerInfo(FrameLib_TimeFormat timeAdvance, bool newFrame, bool outputDone)
Definition: FrameLib_DSP.h:31
FrameLib_TimeFormat getBlockEndTime()
Definition: FrameLib_DSP.h:150
static bool handlesAudio()
Definition: FrameLib_DSP.h:326
unsigned long mMaxBlockSize
Definition: FrameLib_DSP.h:245
ManagedPointer< FrameLib_DSPQueue, &Global::getDSPQueue, &Global::releaseDSPQueue > DSPQueue
Definition: FrameLib_Context.h:73
Definition: FrameLib_DSP.h:339
static ObjectType getType()
Definition: FrameLib_DSP.h:347
void setIO(unsigned long nIns, unsigned long nOuts)
Definition: FrameLib_DSP.h:310
Definition: FrameLib_Types.h:24
bool mOutputDone
Definition: FrameLib_DSP.h:36
FrameType
Definition: FrameLib_Types.h:25
virtual void blockUpdate(double **ins, double **outs, unsigned long blockSize)
Definition: FrameLib_DSP.cpp:52
virtual void reset(double samplingRate, unsigned long maxBlockSize)
Definition: FrameLib_DSP.cpp:68
FrameLib_Context::Allocator mAllocator
Definition: FrameLib_DSP.h:249
FrameLib_AudioOutput(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0, unsigned long nAudioOuts=0)
Definition: FrameLib_DSP.h:344
FrameLib_DSP(ObjectType type, FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans=0)
Definition: FrameLib_DSP.cpp:6
ManagedPointer< FrameLib_LocalAllocator, &Global::getAllocator, &Global::releaseAllocator > Allocator
Definition: FrameLib_Context.h:71
FrameLib_TimeFormat getBlockStartTime()
Definition: FrameLib_DSP.h:149
Definition: FrameLib_Types.h:24
~FrameLib_DSP()
Definition: FrameLib_DSP.cpp:16
FrameLib_Scheduler(FrameLib_Context context, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0, unsigned long nAudioIns=0)
Definition: FrameLib_DSP.h:366
virtual void clearConnections()
Definition: FrameLib_DSP.cpp:116
Definition: FrameLib_DSPQueue.h:12
virtual FrameType outputType(unsigned long idx)
Definition: FrameLib_DSP.h:119
void copyVector(double *output, double *input, unsigned long size)
Definition: FrameLib_DSP.h:170